local skels = {}

local yaozheng = fk.CreateSkill {
  name = "xiaobai__yaozheng",
}

Fk:loadTranslationTable {
  ["xiaobai__yaozheng"] = "爻证",
  [":xiaobai__yaozheng"] = "轮次开始时，你可以将黑色牌和红色牌各一张牌置于牌堆顶，然后你于本轮以下时机可以卜算共计六张牌：准备阶段；出牌阶段限一次；本轮结束时。",
  ["@[desc]xiaobai__yaozheng-round"] = "爻证",
  [":xiaobai__yaozheng-round"] = "本轮以下时机可以卜算共计六张牌：准备阶段；出牌阶段限一次；本轮结束时。",
  ["#xiaobai__yaozheng-invoke"] = "爻证：你可以将黑色牌和红色牌各一张牌置于牌堆顶",
  ["#xiaobai__yaozheng-invoke2"] = "爻证：你可以卜算至多 %arg 张牌",

  ["$xiaobai__yaozheng1"] = "老君举归略，崇本息末矣。",
  ["$xiaobai__yaozheng2"] = "圣人体无，有情而无累也。",
}

yaozheng:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yaozheng.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__yaozheng_active",
      prompt = "#xiaobai__yaozheng-invoke",
      cancelable = true,
      skip = true,
    })
    if success and dat then
      event:setCostData(self, { cards = dat.cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(event:getCostData(self).cards, Card.DrawPile, nil, fk.ReasonPut, yaozheng.name, nil, false, player)
    room:setPlayerMark(player, "@[desc]xiaobai__yaozheng-round", 6)
  end,
})
yaozheng:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@[desc]xiaobai__yaozheng-round") > 0 and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__yaozheng",
      prompt = "#xiaobai__yaozheng-invoke2:::" .. player:getMark("@[desc]xiaobai__yaozheng-round"),
      cancelable = true,
      skip = true,
    })
    if success and dat then
      event:setCostData(self, dat.interaction)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self)
    room:removePlayerMark(player, "@[desc]xiaobai__yaozheng-round", num)
    room:askToGuanxing(player, { cards = room:getNCards(num), skill_name = yaozheng.name })
  end,
})
yaozheng:addEffect("active", {
  prompt = function(self, player, selected_cards, selected_targets)
    return "#xiaobai__yaozheng-invoke2:::" .. player:getMark("@[desc]xiaobai__yaozheng-round")
  end,
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  interaction = function(self, player)
    local mark = player:getMark("@[desc]xiaobai__yaozheng-round")
    if mark > 0 then
      return UI.Spin { from = 1, to = mark }
    end
  end,
  can_use = function(self, player)
    return player:getMark("@[desc]xiaobai__yaozheng-round") > 0 and self:withinTimesLimit(player, Player.HistoryPhase)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local num = self.interaction.data
    room:removePlayerMark(player, "@[desc]xiaobai__yaozheng-round", num)
    room:askToGuanxing(player, { cards = room:getNCards(num), skill_name = yaozheng.name })
  end,
})
yaozheng:addEffect(fk.RoundEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@[desc]xiaobai__yaozheng-round") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = player:getMark("@[desc]xiaobai__yaozheng-round")
    room:removePlayerMark(player, "@[desc]xiaobai__yaozheng-round", num)
    room:askToGuanxing(player, { cards = room:getNCards(num), skill_name = yaozheng.name })
  end,
})
table.insert(skels, yaozheng)

local yaozheng_active = fk.CreateSkill {
  name = "xiaobai__yaozheng_active",
}
yaozheng_active:addEffect("active", {
  card_num = 2,
  target_num = 0,
  card_filter = function(self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    return table.every(selected, function(id)
      return card:compareColorWith(Fk:getCardById(id), true)
    end) and (card.color == Card.Black or card.color == Card.Red)
  end,
  target_filter = Util.FalseFunc,
})
table.insert(skels, yaozheng_active)

return skels
